package com.eastjet.maintenance.contrller;

import com.eastjet.maintenance.config.Code;
import com.eastjet.maintenance.config.Result;
import com.eastjet.maintenance.domain.Documentation;
import com.eastjet.maintenance.domain.InboundDetails;
import com.eastjet.maintenance.domain.Ledger;
import com.eastjet.maintenance.domain.NormalWarehousing;
import com.eastjet.maintenance.service.impl.DocumentationServiceImpl;
import com.eastjet.maintenance.service.impl.InboundDetailsServiceImpl;
import com.eastjet.maintenance.service.impl.NormalWarehousingServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/normalwarehousing")
@CrossOrigin(origins = "*")
public class NormalWarehousingConroller {

    @Autowired
    private NormalWarehousingServiceImpl normalWarehousingService;// 注入NormalWarehousingServiceImpl
    @Autowired
    private InboundDetailsServiceImpl inboundDetailsService;// 注入InboundDetailsServiceImpl
    @Autowired
    private DocumentationServiceImpl documentationService;// 注入DocumentationServiceImpl

    /*
     * 新增正常入库单
     */
    @PostMapping("/insertru")
    public Result InsertNormalWarehousing(@RequestBody Map<String, Object> map) {
        System.out.println("新增正常入库单" + map.keySet());
        List<Object> list = (List<Object>) map.get("tableData");
        list.forEach(item -> {
            System.out.println("ruk" + item);
        });
        Boolean b = normalWarehousingService.InsertNormalWarehousing(map);
        return new Result(b, b ? Code.SAVE_OK : Code.SAVE_ERR, b ? "保存成功" : "保存失败");
    }

    @RequestMapping("/deleteru")
    public Result DeleteNormalWarehousing(@RequestBody List<NormalWarehousing> list) {
        Boolean b = normalWarehousingService.DeleteNormalWarehousing(list);
        return new Result(b, b ? Code.DELETE_OK : Code.DELETE_ERR, b ? "删除成功" : "删除失败");
    }

    @RequestMapping("/selectru")
    public Result SelectNormalWarehousing(NormalWarehousing normalWarehousing, @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum, @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<NormalWarehousing> normalWarehousings = normalWarehousingService.SelectNormalWarehousing(normalWarehousing);
        PageInfo<NormalWarehousing> pageInfo = new PageInfo<>(normalWarehousings);
        return new Result(pageInfo, normalWarehousings.isEmpty() ? Code.GET_ERR : Code.GET_OK, normalWarehousings.isEmpty() ? "查询结果为空" : "查询成功");
    }

    @RequestMapping("/selectrudan")
    public Result SelectNormalWarehousingcha(NormalWarehousing normalWarehousing) {
        System.out.println("查看" + normalWarehousing.getCode());
        List<NormalWarehousing> normalWarehousings = normalWarehousingService.SelectNormalWarehousing(normalWarehousing);
        List<InboundDetails> inboundDetails = inboundDetailsService.GetAllRu(normalWarehousing.getCode());
        List<Documentation> documentations = documentationService.SelectDocumentation(normalWarehousing.getCode());
        Map<String, Object> map = new HashMap<>();
        map.put("normalWarehousings", normalWarehousings);
        map.put("inboundDetails", inboundDetails);
        map.put("documentations", documentations);
        return new Result(map, normalWarehousings.isEmpty() ? Code.GET_ERR : Code.GET_OK, normalWarehousings.isEmpty() ? "查询结果为空" : "查询成功");
    }
}
